package com.wf.love.dao;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.wf.common.util.DateUtil;
import com.wf.love.entity.LoveShow;

@Repository
public class LoveShowDaoImpl implements LoveShowDao{
	@Autowired
	JdbcTemplate jdbcTemplate;

	public boolean addLoveShow(LoveShow loveShow) {
		String sql="INSERT INTO loveShow (loveId,content,title,ip,ipregion,addTime,nickname) VALUES (?,?,?,?,?,?,?)";
		jdbcTemplate.update(sql,loveShow.getLoveId(),loveShow.getContent(),loveShow.getTitle(),loveShow.getIp(),loveShow.getIpregion()
				,new Timestamp(System.currentTimeMillis()),loveShow.getNickname());
		return true;
	}

	@SuppressWarnings("rawtypes")
	public List<LoveShow> getLoveShows(int page,int pageSize) {
		List<LoveShow> loveList=new ArrayList<LoveShow>();
		try {
			String sql="SELECT loveId,nickname,content,title,ipregion,addTime FROM loveShow ORDER BY addTime DESC LIMIT ?,?";
			List<Map<String,Object>> loves=jdbcTemplate.queryForList(sql,new Object[]{(page-1)*pageSize,pageSize});  
			LoveShow info=null;
			for (Map map : loves) {
			    info=new LoveShow();
			    info.setAddTime(DateUtil.convert2long(String.valueOf(map.get("addTime")), "yyyy-MM-dd HH:mm:ss"));
			    info.setContent(String.valueOf(map.get("content")));
			    info.setIp(String.valueOf(map.get("ip")));
			    info.setIpregion(String.valueOf(map.get("ipregion")));
			    info.setLoveId(String.valueOf(map.get("loveId")));
			    info.setNickname(String.valueOf(map.get("nickname")));
			    info.setTitle(String.valueOf(map.get("title")));
			    loveList.add(info);
			}
		} catch (Exception e) {
			System.out.println(e);
		}
		return loveList;
	}
}
